package Help.Manager.Album;

import java.util.ArrayList;


import com.example.easynote.R;


import DTO.AlbumModel;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/*
 * AlbumsAdapter.
 */
public class AlbumsAdapter extends BaseAdapter{

	ArrayList<AlbumModel> albums;
	Context context;
	LayoutInflater inflater;
	float width;
	GridView grid;
	
	public AlbumsAdapter(Context context, ArrayList<AlbumModel> albums, GridView grid, float width) {
		this.albums = albums;
		this.context = context;
		this.width = width;
		this.grid = grid;
		
		this.inflater = LayoutInflater.from(this.context);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View albumView = convertView;
		
		AlbumHolder holder = null;
		
		if(albumView == null) {
			albumView = inflater.inflate(R.layout.album_item, null);
			
			holder = new AlbumHolder();
			holder.image = (ImageView)albumView.findViewById(R.id.img);
			holder.tv = (TextView)albumView.findViewById(R.id.tv);
			holder.v = (View)albumView.findViewById(R.id.rectView);
			
			albumView.setTag(holder);
		}
		else
		{
			holder = (AlbumHolder)albumView.getTag();
		}
		
		AlbumModel album = this.albums.get(position);
		if(!album.hasPhoto())
			return null;
		
		this.width = LayoutHelpers.getColumnWidth(
				this.context, 
				AlbumsFragment.MIN_COL_WIDTH, 
				AlbumsFragment.SPACE_WIDTH,
				AlbumsFragment.MARGIN);
		
		if(position == 0)
			grid.setColumnWidth((int)this.width);
		
		holder.image.setLayoutParams(
				new RelativeLayout.LayoutParams((int)width, (int)width));
		
		BitmapHelpers.loadBitmap(
				album.getFirstPhoto().getPath(), 
				holder.image, 
				this.width, 
				this.width);
		
		holder.tv.setText(album.getTitleWithCount());
		
		return albumView;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.albums.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.albums.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	class AlbumHolder {
		ImageView image;
		TextView tv;
		View v;
	}

}
